/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dmtrsokolov.mbean;

import com.dmtrsokolov.viewbean.Good;
import com.dmtrsokolov.viewbean.PurchaseItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Dmitry
 */

@ManagedBean(name="sessionBean")
@SessionScoped
public class SessionBean implements Serializable {
    
    private Map<Long,Good> priceList;
    private Map<Long,PurchaseItem> cart;
    private int increment = 1;
    
    private Long currentItem;
    private String sortColumnName = "ID";
    private boolean ascending = true;
    
    private boolean editable = false;
    private Good currentGood;// = new Good(1L, "Ford Focus 3", "This is a fake description for Ford Focus 3", 25000.0, 3);
    
    private Comparator<Good> comparator = new Comparator() {
        @Override
        public int compare(Object o1, Object o2) {
           Good g1 = (Good)o1;
           Good g2 = (Good)o2;
           if(sortColumnName == null) return 0;
           if("ID".equals(sortColumnName)) {
               return ascending ? g1.getId().compareTo(g2.getId()) : g2.getId().compareTo(g1.getId());
           } else if("Name".equals(sortColumnName)) {
               return ascending ? g1.getName().compareTo(g2.getName()) : g2.getName().compareTo(g1.getName());
           } else if("Price".equals(sortColumnName)) {
               return ascending ? g1.getPrice().compareTo(g2.getPrice()) : g2.getPrice().compareTo(g1.getPrice());
           } else if("Count".equals(sortColumnName)) {
               return ascending ? g1.getCount().compareTo(g2.getCount()) : g2.getCount().compareTo(g1.getCount());
           }

           return 0;
        }
    };

    public SessionBean() {
        priceList = new HashMap<Long,Good>();
        cart = new HashMap<Long,PurchaseItem>();
        priceList.put(1L,new Good(1L, "Ford Focus 3", "This is a fake description for Ford Focus 3", 25000.0, 3));
        priceList.put(2L,new Good(2L, "Mazda 3", "This is a fake description for Mazda 3", 26500.0, 2));
        priceList.put(3L,new Good(3L, "Renualt Megane 2", "This is a fake description for Mazda 3", 27000.0, 5));
        priceList.put(4L,new Good(4L, "Opel Astra", "This is a fake description for Mazda 3", 27500.0, 2));
        priceList.put(5L,new Good(5L, "Mitsubishi Lanser X", "This is a fake description for Mazda 3", 28000.0, 5));
        priceList.put(6L,new Good(6L, "Subaru Legacy", "This is a fake description for Mazda 3", 29000.0, 2));
        priceList.put(7L,new Good(7L, "VolksWagen Golf", "This is a fake description for Mazda 3", 26000.0, 4));
        priceList.put(8L,new Good(8L, "Shevrole Cruz", "This is a fake description for Mazda 3", 24000.0, 2));
        priceList.put(9L,new Good(9L, "Audi A4", "This is a fake description for Mazda 3", 35000.0, 1));
        priceList.put(10L,new Good(10L, "Honda Civic", "This is a fake description for Mazda 3", 30000.0, 3));
        currentGood = priceList.get(1L);
    }

    public List<PurchaseItem> getCart() {
        return new ArrayList<PurchaseItem>(cart.values());
    }

    public void setCart(Map<Long,PurchaseItem> cart) {
        this.cart = cart;
    }

    public List<Good> getPriceList() {
        List<Good> result = new ArrayList<Good>(priceList.values());
        Collections.sort(result, comparator);
        return result;
    }

    public void setPriceList(Map<Long,Good> priceList) {
        this.priceList = priceList;
    }

    public boolean isAscending() {
        return ascending;
    }

    public void setAscending(boolean ascending) {
        this.ascending = ascending;
    }

    public String getSortColumnName() {
        return sortColumnName;
    }

    public void setSortColumnName(String sortColumnName) {
        this.sortColumnName = sortColumnName;
    }

    public Long getCurrentItem() {
        return currentItem;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public Good getCurrentGood() {
        return currentGood;
    }

    public void setCurrentGood(Good currentGood) {
        this.currentGood = currentGood;
    }
    
    public void addPurchaseItem() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        Long id = Long.valueOf(params.get("id"));
        PurchaseItem item = cart.get(id);
        if(item == null) {
            Good g = priceList.get(id);
            item = new PurchaseItem(g.getId(), increment++, g.getName(), g.getPrice(), 1, null); 
        } else {
            item.setCount(item.getCount()+1);
        }
        item.setSum(item.getCount() * item.getPrice());
        cart.put(id, item);
    }
    
    public void deletePurchaseItems() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        Long id = Long.valueOf(params.get("id"));
        cart.remove(id);
        increment = 1;
        for(PurchaseItem item : cart.values()) {
            item.setNumber(increment++);
        }
    }
    
    public void setCurrentItem() {
        try {
            FacesContext fc = FacesContext.getCurrentInstance();
            Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
            Long id = Long.valueOf(params.get("id"));
            currentItem = id;
            this.editable = true;
            Good good = priceList.get(id);
            currentGood = (Good)good.clone();
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(SessionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void closePopup() {
        editable = false;
    }
    
    public void saveCurrentGood() {
        priceList.put(currentGood.getId(), currentGood);
        editable = false;
    }
    
    public String viewDescription() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        currentItem = Long.valueOf(params.get("id"));
        currentGood = priceList.get(currentItem);
        return "descr";        
    }
    
    
  
    
}
